package com.own.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.own.system.entity.Orders;
import com.own.system.entity.Provider;
import com.own.system.service.OrdersService;
import com.own.system.service.ProviderService;
import com.own.system.utils.Result;
import com.own.system.vo.query.OrdersQueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author anonymous
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/api/order")
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private ProviderService providerService;

    /**
     * 获取订单列表
     * @param ordersQueryVo
     * @return
     */
    @GetMapping("/list")
    public Result list(OrdersQueryVo ordersQueryVo) {
        //创建分页器对象
        IPage<Orders> page = new Page<Orders>(ordersQueryVo.getPageNo(), ordersQueryVo.getPageSize());
        //调用分页查询方法
        ordersService.findOrdersListByPage(page, ordersQueryVo);
        return Result.ok(page);
    }

    /**
     * 通过用户id查询订单
     * @param ordersQueryVo
     * @return
     */
    @GetMapping("/listByUserId")
    public Result listByUserId(OrdersQueryVo ordersQueryVo) {
        //创建分页器对象
        IPage<Orders> page = new Page<Orders>(ordersQueryVo.getPageNo(), ordersQueryVo.getPageSize());
        //调用分页查询方法
        ordersService.findOrdersListByUserId(page, ordersQueryVo);
        return Result.ok(page);
    }

    /**
     * 添加订单
     * @param orders
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Orders orders) {
        Provider provider = providerService.getById(orders.getProviderId());
        provider.setPrice(1);
        providerService.updateById(provider);
        if (ordersService.save(orders)){
            return Result.ok().message("订单添加成功");
        }
        return Result.error().message("订单添加失败");
    }

    /**
     * 发货请求
     * @param orders
     * @return
     */
    @PutMapping("/send")
    @CrossOrigin
    @PreAuthorize("hasAuthority('sys:order:send')")
    public Result send(@RequestBody Orders orders) {
        orders.setOrderStatus(3);
        if (ordersService.updateById(orders)){
            return Result.ok().message("订单发货成功");
        }
        return Result.error().message("订单发货失败");
    }

    /**
     * 支付订单
     * @param orders
     * @return
     */
    @PutMapping("/pay")
    @CrossOrigin
    public Result pay(@RequestBody Orders orders) {
        orders.setOrderStatus(1);
        if (ordersService.updateById(orders)){
            return Result.ok().message("订单支付完成");
        }
        return Result.error().message("订单支付失败");
    }
    /**
     * 支付订单
     * @param orders
     * @return
     */
    @PutMapping("/submit")
    @CrossOrigin
    public Result submit(@RequestBody Orders orders) {
        orders.setOrderStatus(4);
        Provider provider = providerService.getById(orders.getProviderId());
        provider.setPrice(0);
        providerService.updateById(provider);
        if (ordersService.updateById(orders)){
            return Result.ok().message("离开自习室成功");
        }
        return Result.error().message("离开自习室失败");
    }
    /**
     * 删除订单
     * @param id
     * @return
     */
    @CrossOrigin
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:order:delete')")
    public Result delete(@PathVariable Long id) {
        if (ordersService.removeById(id)){
            return Result.ok().message("订单删除成功");
        }
        return Result.error().message("订单删除失败");
    }
}
